using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

/// <summary>
/// Summary description for messageUtils
/// </summary>
public class messageUtils
{
	public messageUtils()
	{

	}
    public static String newMsgCount(message myMessage)
    {
        String msgCount = "NaN";
        
        StringBuilder myQuery = new StringBuilder();
        String dbConn = ConfigurationManager.ConnectionStrings["WDPAConnectionString1"].ConnectionString;
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(dbConn);
        System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand();

        myQuery.Append("select count(*) as msgCount from message where messageto = '");
        myQuery.Append(myMessage.MessageTo);
        myQuery.Append("'");
        
        try
        {
               
            conn.Open();
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = myQuery.ToString();
            
            System.Data.SqlClient.SqlDataReader resObj = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);

            while(resObj.Read())
            {
                msgCount = resObj[0].ToString();
            }
        }
        catch
        {
            // need to log error somewhere
            msgCount = "NaN";
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
        }

        return msgCount;
        
    }
    public static void deleteMessage(Int32 MsgId)
    {
        StringBuilder myDeleteQuery = new StringBuilder();
        String dbConn = ConfigurationManager.ConnectionStrings["WDPAConnectionString1"].ConnectionString;
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(dbConn);
        System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand();

        myDeleteQuery.Append("delete from message where id = ");
        myDeleteQuery.Append(MsgId);
        try
        {
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = myDeleteQuery.ToString();
            Int32 rowsAffected = sqlCommand.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
        }
    }
}
